﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using log4net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.BLL;
using System.Xml;

namespace WEB.APP.Modules.System
{
    public partial class AutoGroup : BasePage 
    {
        public AutoGroup()
            : base(SCREENS.AUTOGROUP, false)
        {

        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            hdScreenDetailID.Value = SCREENS.LISTPROJECT;
            base.Page_Load(sender, e);
        }

        protected void stStudent_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                var _ett = new StudentEntity()
                {
                    Start = Util.Obj2Int(e.Parameters["start"]),
                    HaveProject = false
                };
                _ett.EndNum = _ett.Start + Util.Obj2Int(e.Parameters["limit"]);
                List<StudentEntity> _resul = (List<StudentEntity>)StudentService.GetInstance().LoadHaveProject(_ett);
                if (_resul == null)
                    return;
                stStudent.DataSource = _resul;
                stStudent.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        protected void stProject_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            var _ett = new ProjectEntity()
            {
                Start = Util.Obj2Int(e.Parameters["start"])
            };
            _ett.EndNum = Util.Obj2Int(e.Parameters["limit"]) + _ett.Start;

            IList<ProjectEntity> _lst = ProjectService.GetInstance().LoadNoStudent(_ett);

            if (null != _lst)
            {
                stProject.DataSource = _lst;
                stProject.DataBind();
                e.Total = _ett.TotalRow;
            }
        }

        protected void stGroupProject_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            var _ett = new StudentEntity()
            {
                Start = 0,
                EndNum = 1000000000,
                HaveProject = true
            };

            IList<StudentEntity> _lst = StudentService.GetInstance().LoadHaveProject(_ett);

            if (null != _lst)
            {
                stGroupProject.DataSource = _lst;
                stGroupProject.DataBind();
                e.Total = _ett.TotalRow;
            }
        }

        public void btnAutoGroup_Click(object sender, DirectEventArgs e)
        {
            var _ett = ProjectService.GetInstance().UpdateStudentAuto();
            if (_ett == null)
                return;
            Notification(string.Format("{0} student has been granted project.", _ett.NumberStudent));

            stGroupProject.DataBind();
            stStudent.DataBind();
            stProject.DataBind();
        }

        #region export

        protected void grdGroupProject_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<records></records>");
            XmlNode xml = doc.DocumentElement;

            var StudentE = new StudentEntity()
            {
                HaveProject = true
            };
            var StudentS = StudentService.GetInstance();

            int _Start = 0;
            int _Num = 200;
            int _TotalRow = 1;
            while (_Start < _TotalRow)
            {
                StudentE.Start = _Start;
                StudentE.EndNum = _Start + _Num;

                var lstGroupProject = StudentS.LoadHaveProject(StudentE);

                if (null != lstGroupProject)
                {
                    foreach (var item in lstGroupProject)
                    {
                        xml.AppendChild(CreateEleRecord(doc, item));
                    }
                    _TotalRow = StudentE.TotalRow;
                }
                _Start += _Num;
            }

            ExportCSV(xml, "GroupProject");
        }

        protected XmlElement CreateEleRecord(XmlDocument doc, StudentEntity Entity)
        {
            XmlElement _EleRecord = doc.CreateElement("record");

            XmlElement _EleValue = doc.CreateElement("PersonId");
            _EleValue.InnerText = Entity.StudentId.ToString();
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("LocationCd");
            _EleValue.InnerText = Entity.LocationId;
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("BirthDt");
            _EleValue.InnerText = Entity.BirthDt.Value.ToString("MM/dd/yyyy");
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("Sex");
            _EleValue.InnerText = Entity.Sex == true ? "M" : "F";
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("Surname");
            _EleValue.InnerText = Entity.SurName;
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("GivenNames");
            _EleValue.InnerText = Entity.GivenName;
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("Gpa");
            _EleValue.InnerText = Entity.Gpa.ToString();
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("Gpa");
            _EleValue.InnerText = Entity.Gpa.ToString();
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("ProjectId");
            _EleValue.InnerText = Entity.ProjectId.ToString();
            _EleRecord.AppendChild(_EleValue);

            _EleValue = doc.CreateElement("ProjectName");
            _EleValue.InnerText = Entity.ProjectName.Replace("\"","\"\"");
            _EleRecord.AppendChild(_EleValue);

            return _EleRecord;
        }
        #endregion
    }
}
